//
//  NSDate+DateExtend.m
//  WhatWeather
//
//  Created by byn on 16/3/14.
//  Copyright © 2016年 byn. All rights reserved.
//

#import "NSDate+DateExtend.h"

@implementation NSDate (DateExtend)

- (NSDateComponents *)dateComponents {
    return objc_getAssociatedObject(self.dateComponents, _cmd);
}

- (void)setDateComponents:(NSDateComponents *)dateComponents {
    if (self.dateComponents == nil) {
        self.dateComponents = [[NSDateComponents alloc] init];
    }
    objc_setAssociatedObject(self.dateComponents, _cmd, self, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

+ (NSDate *)initDateWithYear:(NSInteger)year month:(NSInteger)month day:(NSInteger)day hour:(NSInteger)hour minite:(NSInteger)minite second:(NSInteger)second calendarIdentifier : (NSString *)identifier {
    NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
    dateComponents.year = year;
    dateComponents.month = month;
    dateComponents.day = day;
    dateComponents.hour = hour;
    dateComponents.minute = minite;
    dateComponents.second = second;
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:identifier];
    NSDate *date = [calendar dateFromComponents:dateComponents];
    date.dateComponents = dateComponents;
    return date;
}

- (instancetype)createDateWithDataStr: (NSString *)dateStr withDataFormatter : (NSString *)dataFormatter {
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    dateFormatter.dateFormat = dataFormatter;
    
    NSDate *date = [dateFormatter dateFromString:dateStr];
    
    NSInteger timeFlag = NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierChinese];
    NSDateComponents *dateComponent = [calendar components:timeFlag fromDate:date];
    
    self.dateComponents = dateComponent;
    
    return date;
}

- (void)addDays : (NSInteger)days {
    
}


- (NSInteger )getYear {
    return self.dateComponents.year;
}
- (NSInteger)getMonth {
    return self.dateComponents.month;
}
- (NSInteger)getDay {
    return self.dateComponents.day;
}
- (NSInteger)getWeekDay {
    return self.dateComponents.weekday;
}
- (NSInteger)getHours {
    return self.dateComponents.hour;
}
- (NSInteger)getMinite {
    return self.dateComponents.minute;
}
- (NSInteger)getSecond{
    return self.dateComponents.second;
}


@end













































